package com.czmh.etc.onlinepubsys.jy.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Common {

    /**
     * @param inputStream
     * @param digestAlgorithm
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @Title: copyStreamWithHashCode
     * @Description: 获取数据流并做摘要
     * @return: String
     */
    public static String copyStreamWithHashCode(InputStream inputStream, String digestAlgorithm) throws IOException, NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance(digestAlgorithm);
        byte[] buff = new byte[4 * 1024];
        int len;
        while ((len = inputStream.read(buff)) != -1) {
            digest.update(buff, 0, len);
        }
//        return byteToHex(digest.digest());
        return Base64.encodeBase64String(digest.digest());
    }
}
